<?php
/*
This script is used to help automate the Football Pool site.
It is written to run everyday from a windows scheduled task, or
linux cron job at 12:30 everyday.
The script check to see if there is currently a game in progress,
if so it sets of an AJAX script to call a PHP function to get games scores/times
every 30 seconds.
If not, it reloads itself at different intervals based on the day. (More
frequently on days with higher possability of games).

*/
require_once("../include/functions.php");
function mygameInProgress($Week)
{
	$SQL = "SELECT * FROM NCAASchedule Where Week=".$Week." AND PickEmailed=0 ORDER BY GameDate, GameTime";
	//Open Record Set using SQL Query
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	do{
		if (($myrow["GameTime"] != "") && ($myrow["GameDate"] != ""))
		{
			$timeAdjust = 5; //Number of hours From GMT + or - to be added
			$timeAdjust += date("I");
			$timeAdjust *= 60;	//60 minutes in an hour
			$timeAdjust *= 60;	//60 seconds in an minute		
			if (
			(date("Ymd",strtotime($myrow["GameDate"])) < date("Ymd")) ||
			(date("Ymd",strtotime($myrow["GameDate"])) == date("Ymd")) &&
			(date("Hi",strtotime($myrow["GameTime"])) < gmdate("Hi",time() - $timeAdjust)))
			{//if game is being played right now.  User cannot make pick anymore.
				echo "GameDate = ".date("Ymd",strtotime($myrow["GameDate"])).", currently ".date("Ymd").".<br />";
				echo "GameTime = ".date("Hi",strtotime($myrow["GameTime"])).", currently ".gmdate("Hi",time() - $timeAdjust).".<br />";
				$SQL = "UPDATE NCAASchedule SET PickEmailed=1 WHERE Week=$Week AND GameNum=".$myrow["GameNum"];
				mysql_query($SQL);
				return true;
			}
		}
	}while($myrow = mysql_fetch_array($result));
	
	return false;
}
?>
<html>
  <head>
    <title>JoshHughes.com NCAA Football Pool</title>
	<script type="text/javascript" src="scrape.js"></script>
  </head>
<body>
<?php
//Connect To Database
dbConnect();
if (!$Weeknum)
{
	//Get Current Week
	$SQL = "SELECT Week FROM NCAASchedule WHERE Winner='-' ORDER BY Week ASC";
	$weekresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	if($myrow = mysql_fetch_array($weekresult))
		$Weeknum = $myrow["Week"];
	else
		$Weeknum = 1;
}

//check to make sure $Weeknum > 1 AND $Weeknum < 21
$Weeknum = checkWeekBounds($Weeknum);
echo "<h2>Week $Weeknum</h2>";
if (mygameInProgress($Weeknum))
{
	$NumMins = 5;
}
else//No games in progress, retry later.
{

	//Write SQL Query to extract specified data
	$SQL = "SELECT * FROM NFLSchedule Where GameDate=".date("Y-m-d");
	//Open Record Set using SQL Query
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	
	$numGames = mysql_num_rows($result);
	
	if ($numGames > 0)
	{
		echo "There are $numGames games today.<br/>";
		$NumMins = 1440;	//One Day (24 hours)
	}
	else
	{
		switch (date("w"))	//Decide how much later to retry
		{
			case 1:	//Monday
				$NumMins = 11;
				break;
			case 2: //Tuesday
				$NumMins = 11;
				break;
			case 6: //Saturday
				$NumMins = 11;
				break;
			default:
				$NumMins = 61;
		}
	}
}

$TimeOutSecs = $NumMins * 60000;
$timeAdjust = 5; //Number of hours From GMT + or - to be added
$timeAdjust += date("I");
$timeAdjust *= 60;	//60 minutes in an hour
$timeAdjust *= 60;	//60 seconds in an minute	
echo "No games in progress <b>".gmdate("l, F jS Y \@ g:i:s A \E\T",time() - $timeAdjust)."</b> ...Trying again in ".round(($NumMins / 60),0)." hour(s) and ".($NumMins % 60)." minute(s).";
?>
<script>	
setTimeout("window.location.reload()", <?= $TimeOutSecs;?>)
</script>

<div id="State"><br/></div>

</body>
</html>
